package com.zelin.gulimall.coupon.service.impl;

import com.zelin.common.to.SkuReDuctionTo;
import com.zelin.gulimall.coupon.entity.MemberPriceEntity;
import com.zelin.gulimall.coupon.entity.SkuLadderEntity;
import com.zelin.gulimall.coupon.service.MemberPriceService;
import com.zelin.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zelin.common.utils.PageUtils;
import com.zelin.common.utils.Query;

import com.zelin.gulimall.coupon.dao.SkuFullReductionDao;
import com.zelin.gulimall.coupon.entity.SkuFullReductionEntity;
import com.zelin.gulimall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;
    @Autowired
    private MemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveInfo(SkuReDuctionTo skuReDuctionTo) {
        //sku的优惠、满减信息：gulimall_sms-》`sms_sku_full_reduction/`sms_sku_ladder`/`sms_member_price`
        //sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuReDuctionTo.getSkuId());
        skuLadderEntity.setAddOther(skuReDuctionTo.getCountStatus());
        skuLadderEntity.setDiscount(skuReDuctionTo.getDiscount());
        skuLadderEntity.setFullCount(skuReDuctionTo.getFullCount());
        if(skuReDuctionTo.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }

        //sms_sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReDuctionTo,skuFullReductionEntity);
        if(skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
            this.save(skuFullReductionEntity);
        }

        //sms_member_price
        List<MemberPriceEntity> memberPriceEntityList = skuReDuctionTo.getMemberPrice().stream().map(m->{
            MemberPriceEntity entity = new MemberPriceEntity();
            entity.setSkuId(skuReDuctionTo.getSkuId());
            entity.setMemberLevelId(m.getId());
            entity.setMemberLevelName(m.getName());
            entity.setMemberPrice(m.getPrice());
            entity.setAddOther(1);
            return entity;

        }).filter(f->f.getMemberPrice().compareTo(new BigDecimal("0")) == 1).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntityList);

    }

}